package cn.wolfcode.rbac.service.Impl;

import cn.wolfcode.rbac.domain.Permission;
import cn.wolfcode.rbac.mapper.PermissionMapper;
import cn.wolfcode.rbac.qo.PermissionQo;
import cn.wolfcode.rbac.service.PermissionService;
import cn.wolfcode.rbac.util.RequiredPermission;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @author xiahuihui
 * #Description PermissionServiceImpl
 * #Date: 2022/7/2 13:44
 */
@Service
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionMapper permissionMapper;

    //Spring容器对象
    @Autowired
    private ApplicationContext context;

    @Override
    public List<Permission> findPermissionAll() {
        List<Permission> permissions = permissionMapper.selectAll();
        return permissions;
    }

    @Override
    public PageInfo<Permission> findByPage(PermissionQo qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Permission> permissions = permissionMapper.selectAll();
        return new PageInfo<Permission>(permissions);
    }

    @Override
    public void deletePermission(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void reload() {
        //获取到数据库权限
        List<Permission> permissions = permissionMapper.selectAll();
        //将Controller中的每一个权限获取到

        //1.从spring容器中获取到所有的Controller对象
        Map<String, Object> beansWithAnnotation = context.getBeansWithAnnotation(Controller.class);

        //2.获取map中所有的值，每个值就是一个Controller对象
        Collection<Object> beans = beansWithAnnotation.values();

        //3.遍历每一个controller，获取对象中所有的方法
        for (Object bean : beans) {
            Method[] methods = bean.getClass().getMethods();
            //遍历出每一个方法，获取方法上面的权限注解
            for (Method method : methods) {
                //判断当前方法上面是否存在权限注解
                if(method.isAnnotationPresent(RequiredPermission.class)){
                    //如果有，获取该注解
                    RequiredPermission annotation = method.getAnnotation(RequiredPermission.class);
                    //获取权限名称和表达式
                    String[] value = annotation.value();
                    String name = value[0];  //权限名
                    String expression = value[1]; //权限表达式

                    //遍历出Controller中每一个权限,判断数据库是否存在权限
                    boolean f = false;//假设不存在
                    for(Permission permission:permissions){
                        if(!permission.getName().equals(name) || !permission.getExpression().equals(expression)){
                            //存在
                            f = true;
                            break;
                        }
                    }
                    //只有在循坏结束后，才能确定不存在
                    if(!f){
                        //不存在
                        //如果没有，添加到数据库中
                        Permission permission1 = new Permission();
                        permission1.setName(name);
                        permission1.setExpression(expression);
                        permissionMapper.insert(permission1);
                    }
                }
            }
        }



    }
}
